//
//  MainTabBarC.swift
//  Employee
//
//  Created by swordjoy on 2017/3/13.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit
import SwiftyJSON
import Moya
//import Hero

class MainTabBarC: UITabBarController {
  
  private var titles: [String] = []
  private var VCs: [UIViewController] = []
  private var images: [String] = []
  private var selectedImages: [String] = []
  private var navs: [SJBaseNavigationC] = []
  
  fileprivate var homeVC = HomeVC()
  private var messageVC = AddressBookVC()//MessageVC()
  private var functionVC = FunctionVC()
  
  fileprivate var orderCancle: Cancellable?
  fileprivate var inspection: Cancellable?
  fileprivate var toolBarRedPoint: UIView!
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    create()
    
    // HX: 未读消息通知
    //GlobalConfig.notificationCenter.addObserver(self, selector: #selector(updateUnreadMessageCount), name: Notification.Name.hxMessage.updateUnreadMessageCount, object: nil)

    // 统计消息数
    if Account.isLogin {
        //updateUnreadMessageCount()
    }
  }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        // 如果没有登录,跳转登录界面
        if !Account.isLogin {
            let longinVC = SJHiddenNavigationC(rootViewController: LoginVC())
            present(longinVC, animated: false, completion: nil)
        }
    }
  
  func create() {
    view.backgroundColor = UIColor.white
    
    titles = ["工作", "功能", "通讯录"]
    VCs = [homeVC, functionVC, messageVC]
    images = ["tab-work-icon-normal", "tab-funtion-icon", "addressbook-unselected"]
    selectedImages = ["tab-work-icon-selected", "tab-funtion-icon-selected", "addressbook-selected"]
    
    for (index, vc) in VCs.enumerated() {
        vc.title = titles[index]
        vc.tabBarItem.image = UIImage(named: images[index])?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named: selectedImages[index])?.withRenderingMode(.alwaysOriginal)
        
        let offset = UIOffsetMake(0, -2);
        vc.tabBarItem.titlePositionAdjustment = offset;
        
        let nav = SJBaseNavigationC(rootViewController: vc)
      
        navs.append(nav)
    }
    
    viewControllers = navs
    
    toolBarRedPoint = UIView()
    toolBarRedPoint.backgroundColor = GlobalConfig.GrapeFruitColor
    
    tabBar.addSubview(toolBarRedPoint)
    toolBarRedPoint.snp.makeConstraints { (make) in
      make.height.width.equalTo(10)
      make.right.equalTo(-46)
      make.bottom.equalTo(-35)
    }
    toolBarRedPoint.layer.cornerRadius = 5
    toolBarRedPoint.clipsToBounds = true
    toolBarRedPoint.isHidden = true
    
  }
  
  
  
  deinit {
    SJApp.log("MainTab deinit")
  }
  
}

